package com.cetcs.kmga.hbase.index;

import com.google.common.collect.Lists;
import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;

import java.io.IOException;
import java.sql.*;
import java.sql.Connection;
import java.util.*;

/**
 * 类注释
 *
 * @author xutao
 * @version V1.0 创建时间：2017-09-13 11:21
 *          Copyright 2017 by CETC
 */
public class TestLj extends TestH2 implements  Test1,Test2{

    public static void main(String[] args) throws IOException, SQLException {

    }

    public static void testLj() throws SQLException {
        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace( );
        }
        String url = "jdbc:phoenix:www.datanode1.zgw,www.datanode2.zgw,www.namenode.zgw:2181:/hbase-unsecure";
        Connection conn = DriverManager.getConnection(url);
        Statement statement = conn.createStatement( );

        String sql = "select count(*) as cn from SDATA_AUDIT_DB_LOG_201709";

        long time = System.currentTimeMillis( );
        ResultSet rs = statement.executeQuery(sql);

        while (rs.next( )) {
            int count = rs.getInt("cn");
            System.out.println("row count is " + count);
        }

        long timeUsed = System.currentTimeMillis( ) - time;
        System.out.println("time " + timeUsed + "mm");
        rs.close( );
        statement.close( );
        conn.close( );
    }

    public static void testPhinex() {
        java.sql.Connection conn = null;
        ResultSet rs = null;
        PreparedStatement statement = null;
        Map<String, Object> map = null;
        try {
            conn = DruidPool.getInstance( ).getConnection( );
            String sqlBuilder = ("select * from kmga.SDATA_AUDIT_DB_LOG_009_201710 where ID = ?");
            statement = conn.prepareStatement(sqlBuilder.toString( ));
            statement.setString(1, "00292233705300704338071000000299");
            rs = statement.executeQuery( );
            ResultSetMetaData data = rs.getMetaData( );
            while (rs.next( )) {
                map = new HashMap<>( );
                for (int i = 1; i <= data.getColumnCount( ); i++) {// 数据库里从 1 开始
                    String c = data.getColumnName(i).toUpperCase( );
                    Object v = rs.getObject(c);
                    if (v != null) {
                        map.put(c, v);
                        System.out.println(c + "===" + v);
                    }
                }
            }
            rs.close( );
        } catch (SQLException ex) {
            ex.printStackTrace( );
            map = null;
        } finally {
            try {
                if (rs != null) {
                    rs.close( );
                }
                if (statement != null) {
                    statement.close( );
                }
            } catch (SQLException ex) {
                ex.printStackTrace( );
            }

            DruidPool.close(conn);
        }


    }

    public static void testConfig() {
        Configuration configuration = HBaseConfiguration.create( );
        configuration.addResource(new Path("hbase-site.xml"));
        configuration.addResource(new Path("core-site.xml"));
        configuration.set("hbase.config.read.zookeeper.config", "true");
        String[] strS = configuration.getStrings("hbase.zookeeper.quorum");
        for (String st : strS) {
            System.out.println(st);
        }
        System.out.println(configuration.getBoolean("hbase.config.read.zookeeper.config", false));
    }

    public static void testQuery() throws IOException, DateParseException {
        long startTimeL = DateUtil.parseDate("2017-11-01").getTime( );
        long endTimeL = new java.util.Date( ).getTime( );
        long max = Long.MAX_VALUE - startTimeL;//起始
        long min = Long.MAX_VALUE - endTimeL;//结束时间


    }

    public static Map<String, Object> result2Map(Result result) {
        Map<String, Object> ret = new HashMap<>( );
        if (result != null && result.listCells( ) != null) {
            String rowKey = "";
            for (Cell cell : result.listCells( )) {
                String key = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                ret.put(key, value);
                rowKey = Bytes.toString(cell.getRowArray( ), cell.getRowOffset( ), cell.getRowLength( ));
            }

            ret.put("ROW_KEY", rowKey);
        }
        return ret;
    }
}
